// START SNIPPET: mSurvey_common_mjs 
// Url to use to submit the questionaire answers
var submitUrl;

// Array of question nodes
var questions;

// The currently rendered question
var curQuestion = 0;

// Array containing the answers to each of the questions
var answers = new Array();

/*-------------------------------------
 * This function loads a questionaire 
 * posted to a web server.
 *-------------------------------------*/
function init() {
	if (questions) {
		show(questions[0]);
	} else {
		show(loading, 1);
		var loadRequest = new HttpRequest();
		loadRequest.onLoaded = function() { 
			var questionaire = this.responseXML; 
			submitUrl = questionaire.responseUrl;
			questions = questionaire.question;
			loadQuestions();
			hide(loading);
		};
		loadRequest.onError = function() { notify(this.statusText); };
		loadRequest.open("GET", "Questionaire.xml");
		loadRequest.overrideMimeType("text/xml");
		loadRequest.send();
	}
}

function loadQuestions() {
	if (questions) {
		var ndx = 0;
		for (question in questions) {
			loadQuestion(question, ndx++);
		}
		show(questions[0]);
	} else {
		notify("No Questions to Load!");
	}
}

function loadQuestion(question, ndx) {
	debug(question);
	var screen = new Screen();
	screen.id = "question" + ndx;
	screen.layout="vertical";
	
	// add a banner to the new screen
	screen.push(new banner());
	
	// create the card and add to the new screen
	var c = new card();
	var tmp = ndx+1;
	c.title = "Question " + tmp + " of " + questions.length;
	c.text = question.text;
	
	if (question.type == "choice") {
		var rgroup = new radiogroup();
		c.push(rgroup);
		for (option in question.option) {
			var rbox = new radiobox();
			rbox.label = option.label;
			rbox.value = option.value;
			rgroup.push(rbox);
		}
	} else {
		c.push(new inputbox());
	} 
	
	// add the card to the screen
	screen.push(c);
	
	// create the softkeys for the screen.
	var skeys = new softkeys();
	
	if (ndx == 0) {
		skeys.left = "Exit";
		screen.onLeftSoftkey = function() { exit(); };
	} else {
		skeys.left = "Back";
		screen.onLeftSoftkey = back;
	}	
	
	if (question == questions[questions.length-1]) {
		skeys.right = "Submit Survey";
		screen.onRightSoftkey = submitAnswers;
	} else {
		skeys.right = "Next";
		screen.onRightSoftkey = next;
	}
	
	screen.push(skeys);

	// swap out the XML Node with its screen equivalent
	questions[ndx] = screen;
}

function notify(message) {
	debug(message);
	error.text = message;
	show(error);
}

function back() {
	curQuestion = curQuestion - 1;
	show(questions[curQuestion]);
	repaint();
}

function next() {
	curQuestion = curQuestion + 1;
	show(questions[curQuestion]);
	repaint();
}

function submitAnswers() {
	loading.text = "Uploading ...";
	show(loading, 1);
	var uploadRequest = new HttpRequest();
	uploadRequest.onLoaded = function() { 
		show(finished);
	};
	uploadRequest.onError = function() { notify(this.statusText); };
	uploadRequest.open("POST", submitUrl);
	uploadRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	var params = "";
	var index = 0;
	for (q in questions) {
		debug(q);
		debug(params);
		if (index > 0) {
			params += "&";
		} 
		params = params + "question" + index + "=" + q.children[1].children[2].getAnswer();
		index++;
	}
	uploadRequest.send(params);
}
// END SNIPPET: mSurvey_common_mjs
